/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.emergency.resources.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.emergency.plan.dto.ErEmergencyPlanDTO;
import com.hopes.bizz.emergency.resources.dto.ErEmergencyResourcesObjDTO;
import com.hopes.bizz.emergency.resources.entity.ErMaterialCheckEntity;
import com.hopes.bizz.emergency.resources.service.ErMaterialCheckService;
import com.hopes.bizz.emergency.resources.vo.ErMaterialVO;
import com.hopes.bizz.emergency.support.AboutExpireSupport;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.bizz.emergency.resources.entity.ErMaterialEntity;
import com.hopes.bizz.emergency.resources.service.ErMaterialService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 应急物资
 *
 * @author cx
 * @date 2023-11-01 15:26:20
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/resources/material")
@Tag(description = "material", name = "应急物资管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ErMaterialController {

    private final ErMaterialService erMaterialService;

    /**
     * 分页查询
     *
     * @param page       分页对象
     * @param erMaterial 应急物资
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('resources_material_view')")
    public R getErMaterialPage(Page page, ErMaterialEntity erMaterial) {
        return erMaterialService.erMaterialPage(page,erMaterial);
    }


    /**
     * 通过id查询应急物资
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('resources_material_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(erMaterialService.getById(id));
    }

    /**
     * 通过单元标识查询应急物资
     *
     * @param resourcesObjDTO resourcesObjDTO
     * @return R
     */
    @Operation(summary = "通过单元标识查询", description = "通过单元标识查询")
    @GetMapping("/getListByUnitId")
    public R getListByUnitId(Page page, ErEmergencyResourcesObjDTO resourcesObjDTO) {
        return erMaterialService.getListByUnitId(page,resourcesObjDTO);
    }

    /**
     * 新增应急物资
     *
     * @param erMaterial 应急物资
     * @return R
     */
    @Operation(summary = "新增应急物资", description = "新增应急物资")
    @SysLog("新增应急物资")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('resources_material_add')")
    public R save(@RequestBody ErMaterialEntity erMaterial) {
        AboutExpireSupport.setExpireStatus(erMaterial, ErMaterialEntity::getDueDate,ErMaterialEntity::setSatus);
        return R.ok(erMaterialService.save(erMaterial));
    }

    /**
     * 修改应急物资
     *
     * @param erMaterial 应急物资
     * @return R
     */
    @Operation(summary = "修改应急物资", description = "修改应急物资")
    @SysLog("修改应急物资")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('resources_material_edit')")
    public R updateById(@RequestBody ErMaterialEntity erMaterial) {
        AboutExpireSupport.setExpireStatus(erMaterial, ErMaterialEntity::getDueDate,ErMaterialEntity::setSatus);
        return R.ok(erMaterialService.updateById(erMaterial));
    }

    /**
     * 通过id删除应急物资
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除应急物资", description = "通过id删除应急物资")
    @SysLog("通过id删除应急物资")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('resources_material_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(erMaterialService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param erMaterial 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('resources_material_export')")
    public List<ErMaterialEntity> export(ErMaterialEntity erMaterial) {
        return erMaterialService.list(Wrappers.query(erMaterial));
    }
}